package com.zhangh.summer.admin.biz.log.impl;

import com.zhangh.summer.admin.support.utils.DateUtil;
import com.zhangh.summer.admin.biz.log.LoginLogBiz;
import com.zhangh.summer.admin.model.bo.LoginLogQuery;
import com.zhangh.summer.admin.model.po.AdLoginLogPO;
import com.zhangh.summer.admin.model.po.AdLoginLogPOExample;
import com.zhangh.summer.admin.support.ds.common.MixedDS;
import com.zhangh.summer.admin.support.ds.common.pager.DataPager;
import com.zhangh.summer.admin.support.ds.common.pager.Pager;
import com.zhangh.summer.admin.support.id.IdGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-10-08 01:49
 **/
@Service
@Slf4j
public class LoginLogBizImpl implements LoginLogBiz {
    @Resource
    private MixedDS mixedDS;
    @Autowired
    private IdGen idGen;

    @Override
    public DataPager<AdLoginLogPO> getLoginLogDataPager(LoginLogQuery query, Pager pager) {
        AdLoginLogPOExample example=new AdLoginLogPOExample();
        AdLoginLogPOExample.Criteria c=example.createCriteria();
        if (StringUtils.isNotBlank(query.getName())){
            c.andLoginNameLike("%"+query.getName()+"%");
        }
        if (query.getLoginTime()!=null&&query.getLoginTime().size()==2){
            Date start= DateUtil.parseFull(query.getLoginTime().get(0));
            Date end= DateUtil.parseFull(query.getLoginTime().get(1));
            c.andLoginTimeBetween(start,end);
        }
        AdLoginLogPOExample.Criteria or=example.or();
        if (StringUtils.isNotBlank(query.getName())){
            or.andUserNameLike("%"+query.getName()+"%");
        }
        if (query.getLoginTime()!=null&&query.getLoginTime().size()==2){
            Date start= DateUtil.parseFull(query.getLoginTime().get(0));
            Date end= DateUtil.parseFull(query.getLoginTime().get(1));
            or.andLoginTimeBetween(start,end);
        }
        example.setOrderByClause("loginTime DESC");
        DataPager<AdLoginLogPO> dataPager=this.mixedDS.txDs().mapper().selectDataPager(example,pager);
        return dataPager;
    }

    @Override
    @Transactional
    public void saveLog(AdLoginLogPO adLoginLogPO) {
        adLoginLogPO.setId(idGen.getID());
        int c1=this.mixedDS.txDs().mapper().insert(adLoginLogPO);
        log.info("保存登录日志 id {}",adLoginLogPO.getId());
    }

    @Override
    public void loginOut(String token) {
        AdLoginLogPOExample example=new AdLoginLogPOExample();
        example.createCriteria().andTokenEqualTo(token);
        AdLoginLogPO adLoginLogPO=new AdLoginLogPO();
        adLoginLogPO.setToken(token);
        adLoginLogPO.setLoginOutTime(new Date());
        int c1=this.mixedDS.txDs().mapper().updateByExampleSelective(adLoginLogPO,example);
        log.info("用户 token {} 登出",token);

    }


}
